﻿using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;

namespace DemoApplication
{
public class LoaderTouchesController : MonoBehaviour {
		bool loadLevelAnimationStarted = false;
		bool getMoreCoinsAnimationPlayed = false;
		bool adsShowing = false;
    	bool showedStartAd = false;

#if UNITY_EDITOR
	string gameId = @"14850";
#elif UNITY_ANDROID
	string gameId = @"20128";
#elif UNITY_IOS
    string gameId = @"14850";
#else 
    string gameId = @"14850";
#endif

	void Start () 
	{
		Advertisement.Initialize(gameId);
	}

	void Update () {
		if (Advertisement.isReady(@"incentivizedZone")) 
		{
			if (!getMoreCoinsAnimationPlayed) {
				getMoreCoinsAnimationPlayed = true;
			}

		} else {
			getMoreCoinsAnimationPlayed = false;
		}

		if(Advertisement.isReady("gameStartZone") && !showedStartAd) 
		{
			showedStartAd = true;
			Advertisement.Show(@"gameStartZone", new ShowOptions {pause = true, resultCallback = null});
		}

		if (Input.GetMouseButtonDown (0) && !Advertisement.isShowing) {
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(ray, out hit)) {
				if (hit.collider.name == @"startButton") {
					loadLevelAnimationStarted = true;
				}

				if (hit.collider.name == @"getMoreCoins" && !adsShowing) {
					adsShowing = true;
					Advertisement.Show(@"incentivizedZone", new ShowOptions {
						pause = true,
						resultCallback = result => {
							adsShowing = false;
							if (result == ShowResult.Finished) {
								//SharedData.coinsCount += 10;
								GUIText guiText = GameObject.Find(@"coinsCounter").GetComponent<GUIText>();
								//guiText.text = SharedData.coinsCount.ToString();
								guiText.transform.position = new Vector3(1 - guiText.GetScreenRect().width/Screen.width - 0.06f,
								                                         0.98f, 
								                                         0);
							}
						}
					});
				}
			}
		}

		if (loadLevelAnimationStarted) {
		
		}
	}
}
}
